<?php
class MY_Controller extends CI_Controller {

	var $viewData;

	public function __construct() {
		parent::__construct();

		$this->viewData = array();

		$this->viewData['title'] = 'Universo Delivery';
		$this->viewData['css'] = array();
		$this->viewData['scripts'] = array();

		if($this->isPessoaLoggedIn() || $this->isEmpresaLoggedIn()){
			$this->setViewData('user_info', $this->getLoggedUser());
		}
	}

	protected function setLoggedUser($email, $nome, $tipo, $codAuto){

		//
		// Tipo: 1: Empresa
		//       2: Pessoa
		//       3: Administrativo do site
		//

		$info = array(
			'username'  => $nome,
			'email'     => $email,
			'tipo'	    => $tipo,
			'cod'		=> $codAuto,
			'logged_in' => true
			);

		$this->session->set_userdata($info);
	}

	protected function getLoggedUser(){
		return array(
			'username'  => $this->session->userdata('username'),
			'email'     => $this->session->userdata('email'),
			'tipo'	    => $this->session->userdata('tipo'),
			'cod'	    => $this->session->userdata('cod'),
			'logged_in' => $this->session->userdata('logged_in')
			);
	}

	protected function logoff(){
		$this->session->set_userdata('logged_in', false);
	}

	protected function isEmpresaLoggedIn(){
		return $this->isLoggedIn(1);
	}

	protected function isPessoaLoggedIn(){
		return $this->isLoggedIn(2);
	}

	protected function isLoggedIn($tipo){
		if($this->session->userdata('tipo') && $this->session->userdata('tipo') == $tipo){
			return $this->session->userdata('logged_in');
		}

		return false;
	}

	protected function setTitle($title){
		$this->viewData['title'] = $title;
	}

	protected function setViewData($key, $value){
		$this->viewData[$key] = $value;
	}

	protected function addCss($url) {
		array_push($this->viewData['css'], $url);
	}

	protected function addScript($url) {
		array_push($this->viewData['scripts'], $url);
	}

}